package com.example.oppotest.controller;

import com.example.oppotest.entity.youku.Effpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author wyk
 * @Date: 2025/3/4 23:10
 */

@RestController
@RequestMapping("/taobao")
public class TaobaoController extends BaseController{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/deeplinkExchange", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap deeplinkExchange(@RequestParam("originalUrl") String originalUrl, HttpServletRequest request) {
        ModelMap map = new ModelMap();
        System.out.println(originalUrl);
        try {
            if (!StringUtils.hasText(originalUrl) || (!originalUrl.startsWith("https://m.tb.cn/") && !originalUrl.startsWith("https://e.tb.cn/"))) {
                sendFailureMessage(map, "原始链接不存在或者不符合规则");
                return map;
            }
            String encodedUrl = "tbopen://m.taobao.com/tbopen/index.html?h5Url=" + URLEncoder.encode(originalUrl, "UTF-8");
            map.put("encodedUrl", encodedUrl);
            sendSuccessMessage(map, "成功");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            sendFailureMessage(map, e.getMessage());
        }
        return map;
    }


}
